/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： mysite
 * 文件名称： manager.js
 * Date：18-9-21 上午9:21
 * Author: guomw
 */

$(function () {
    managerHandler.init();
    managerHandler.initRole();
});
var managerHandler = {
    localData: [],
    menusbtnTemplate: $("#menus_btn_template").html(),
    init: function () {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/sys/ajax/managerlist", {}, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                var appendHtml = '';
                if(ret.data==null)
                    return;
                self.localData = ret.data;
                $.each(ret.data, function (i, item) {
                    var tempHtml = $("#table-content-template").html();
                    tempHtml = tempHtml.replace(/{index}/gm, i + 1);
                    tempHtml = tempHtml.replace(/{loginName}/gm, item.loginName);
                    tempHtml = tempHtml.replace(/{roleType}/gm, item.role.roleName);
                    tempHtml=tempHtml.replace(/{enable}/gm,item.enable?"启用":"禁用");
                    if (item.role.roleType != -1) {
                        var btnTempHtml = self.menusbtnTemplate.replace("{clickMethod}", "managerHandler.edit(" + item.userId + ")").replace("{title}", "编辑").replace(/{target}/gm, "#addManagerModal");
                        btnTempHtml += self.menusbtnTemplate.replace("{clickMethod}", "managerHandler.setEnable(" + item.userId + ")").replace("{title}", item.enable ? "禁用" : "启用").replace(/{target}/gm, "");
                        tempHtml = tempHtml.replace(/{btnItems}/, btnTempHtml);
                    }

                    appendHtml += tempHtml;
                });
                $("#table-content").html(appendHtml);
            }
        });
    },
    getModel: function (dataId) {
        var result = {};
        $.each(this.localData, function (i, item) {
            if (item.userId === dataId) {
                result = item;
                return false;
            }
        });
        return result;
    },
    /**
     * 加载角色数据
     */
    initRole: function () {
        hotUtil.ajaxCall("/sys/ajax/rolelist", {}, function (ret) {
            if (ret.resultCode === 2000) {
                var appendHtml = '<option value="-1">--请选中所属角色--</option>';
                $.each(ret.data, function (i, item) {
                    if (item.roleType == 0) {
                        appendHtml += '<option value="' + item.roleId + '">' + item.roleName + '</option>';
                    }
                });
                $("#userRole").html(appendHtml);
            }
        });
    },
    btnSubmit: function () {
        var self=this;
        if (hotUtil.isNullOrEmpty($.trim($("#loginName").val()))) {
            swal("请输入账户名", "", "warning")
            $("#loginName").focus();
            return false;
        }
        var roleId=$("#userRole").val();
        if (parseInt(roleId)<=0) {
            swal("请选中所属角色", "", "warning")
            return false;
        }
        hotUtil.loading.show();
        hotUtil.ajaxCall("/sys/ajax/editmanager", {
            rootId:parseInt($("#userId").val()),
            loginName:$.trim($("#loginName").val()),
            loginPwd:$.trim($("#loginPwd").val()),
            roleId:roleId
        }, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                self.init();
                swal(ret.resultMsg, "", "success");
                $('#addManagerModal').modal('hide');
            }
            else {
                swal(ret.resultMsg, "", "warning")
            }
        });
    },
    edit: function (dataId) {
        var loginName = "";
        var roleId=-1;
        $("#loginName").removeAttr("disabled");
        if (dataId > 0) {
            var data = this.getModel(dataId);
            if (data != null) {
                loginName = data.loginName;
                roleId=data.role.roleId;
                $("#loginName").attr("disabled","disabled");
            }
            else {
                dataId = 0;
            }
        }
        $("#modal-manager-title").text(dataId > 0 ? "编辑账户信息" : "添加账户")
        $("#userId").val(dataId);
        $("#loginName").val(loginName);
        $("#userRole").val(roleId);
    },
    /**
     * 修改状态
     * @param dataId
     */
    setEnable: function (dataId) {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/sys/ajax/setuserenable", {
            rootId: dataId
        }, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                self.init();
                swal("设置成功", "", "success");
            }
            else {
                swal("设置失败", "", "error")
            }
        });
    }
}